package com.globalwave.pos.user.service;


import junit.framework.Assert;

import org.junit.Test;

import com.globalwave.common.exception.BusinessException;
import com.globalwave.system.entity.TerminalProfile;
import com.globalwave.system.entity.User;


public class TerminalProfileServiceTest extends PosBaseTestCase {

	@Test
	public void dayBegin_OK() throws Exception {
		TerminalProfile t$r3 = getTerminalProfileR3() ;

		//
		try {
		    getTerminalProfileService().dayBeginAndLogin(t$r3, getUserRT010078()) ;

			Assert.assertFalse("must be throw exception!", true) ;
			
		} catch (BusinessException be) {
			Assert.assertEquals("sale day too early!", "E0001020008", be.getMessageKey()) ;
		}		

		TerminalProfile t$r1 = getTerminalProfileR1() ;
		// common day begin
		t$r1.setSale_date(getDate("20101001")) ;
		getTerminalProfileService().dayBeginAndLogin(t$r1, getUserRT010078()) ;
		
		TerminalProfile t = getTerminalProfileService().get(regionCode, terminalCode) ;
		
		TerminalProfile exp = getTerminalProfileR1() ;
		exp.setCurrent_user(userId) ;
		
		assertObject("after day begin", exp, t, new String[]{"computer_ip", "computer_name", "current_user"}) ;
		
		
        // can't not open 2 times!!
		try {
		    getTerminalProfileService().dayBeginAndLogin(t$r1, getUserRT010078()) ;

			Assert.assertFalse("must be throw exception!", true) ;
			
		} catch (BusinessException be) {
			Assert.assertEquals("status is open!", "E0001020003", be.getMessageKey()) ;
		}
		
		// can't not login 2 diff pos!!
		try {
		    getTerminalProfileService().login(getTerminalProfileR2(), getUserRT010078()) ;

			Assert.assertFalse("must be throw exception!", true) ;
			
		} catch (BusinessException be) {
			Assert.assertEquals("user have login another pos!", "E0001020005", be.getMessageKey()) ;
		}
	}
	

	@Test
	public void dayEnd_OK() throws Exception {

		TerminalProfile t$r1 = getTerminalProfileR1() ;

	    getTerminalProfileService().dayEnd(t$r1) ;	
	    
	    TerminalProfile t = getTerminalProfileService().get(regionCode, terminalCode) ;
	    
		TerminalProfile exp = new TerminalProfile() ;
		
		assertObject("after day end", exp, t, new String[]{"computer_ip", "computer_name", "current_user"}) ;
		
		// can't not close 2 times!!
		try {
		    getTerminalProfileService().dayEnd(t$r1) ;

			Assert.assertFalse("must be throw exception!", true) ;
			
		} catch (BusinessException be) {
			Assert.assertEquals("status is close!", "E0001020004", be.getMessageKey()) ;
			//System.err.println(be.getLocalizedMessage());
		}
	}
	
	private TerminalProfile getTerminalProfileR1() throws Exception {
		TerminalProfile e = new TerminalProfile() ;
		e.setRegion_code(regionCode) ;
		e.setCode_(terminalCode) ;
		e.setSale_date(getDate("20101001")) ;
		e.setComputer_ip("192.168.0.99") ;
		e.setComputer_name("Test XP") ;
		return e ;
	}

	private TerminalProfile getTerminalProfileR2() throws Exception {
		TerminalProfile e = new TerminalProfile() ;
		e.setRegion_code(regionCode) ;
		e.setCode_(terminalCodeR2) ;
		e.setSale_date(getDate("20101001")) ;
		e.setComputer_ip("192.168.0.98") ;
		e.setComputer_name("Test XP-2") ;
		return e ;
	}


	private TerminalProfile getTerminalProfileR3() throws Exception {
		TerminalProfile e = new TerminalProfile() ;
		e.setRegion_code(regionCode) ;
		e.setCode_(terminalCodeR3) ;
		e.setSale_date(getDate("20101001")) ;
		e.setComputer_ip("192.168.0.98") ;
		e.setComputer_name("Test XP-2") ;
		return e ;
	}
	
	
	private User getUserRT010078() {
		User e = new User() ;
		e.setRegion_code(regionCode) ;
		e.setLogin_id(userId) ;
		e.setShop_code(shopCode);

		return e ;
		
	} 
}
